package com.ruoyi.web.controller.tool;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * URL处理器控制器
 */
@Api("URL处理工具")
@RestController
@RequestMapping("/tool/url")
public class UrlProcessorController extends BaseController {

    @Autowired
    private UrlProcessor urlProcessor;

    /**
     * 处理Excel文件中的URL并生成描述
     */
    @ApiOperation("处理Excel中的URL")
    @PostMapping("/process")
    public AjaxResult processUrls(
            @ApiParam(value = "Excel文件", required = true)
            @RequestParam("file") MultipartFile file,

            @ApiParam(value = "URL所在列索引（从0开始）", required = true)
            @RequestParam(value = "urlColumnIndex", defaultValue = "0") Integer urlColumnIndex) {

        if (file.isEmpty()) {
            return AjaxResult.error("请选择一个Excel文件");
        }

        String fileName = file.getOriginalFilename();
        if (fileName == null || (!fileName.toLowerCase().endsWith(".xlsx") && !fileName.toLowerCase().endsWith(".xls"))) {
            return AjaxResult.error("请上传Excel文件（.xlsx或.xls格式）");
        }

        return urlProcessor.processUrls(file, urlColumnIndex);
    }
}